package com.bosicen.crm.web.controller.sysmanage;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.io.Writer;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bosicen.crm.dao.entity.OrderProcess;
import com.bosicen.crm.service.sysmanage.OrderProcessServiceImpl;
import com.bosicen.crm.tools.AppUtil;
import com.google.gson.Gson;

/***
 * 订单流程
 * @author tigerlei
 *
 */
@Controller
@RequestMapping("/basicinfo")
public class OrderProcessController {
	
	@Autowired
	public OrderProcessServiceImpl orderProcessService;
	
	
	/**
	 * 新增,修改信息
	 * @param company
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/addOrderPorcess/{type}")
	public String addorderProcess(HttpServletRequest request,@PathVariable("type") Integer type){
		String id = request.getParameter("id");
		OrderProcess orderProcess = null;
		if(type != 0 && StringUtils.isNotBlank(id)){
			orderProcess = orderProcessService.findOrderProcessById(AppUtil.ObjectToInt(id));
		}
		request.setAttribute("orderProcess", orderProcess);
		return "sysmanage/addOrderPorcess";
	}
	
	
	/**
	 * 保存新增/修改、删除信息
	 * 
	 * @param orderProcess
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/cudOrderPorcess/{type}")
	public void saveOrderPorcess(OrderProcess orderProcess, HttpServletRequest request, @PathVariable("type") Integer type, Writer writer) {
		boolean flag = false;
		Gson gson = new Gson();
		try {
			switch (type) {
			case 0:
				flag = orderProcessService.delOrderProcess(orderProcess);
				break;
			case 1:
				flag = orderProcessService.saveOrUpdateOrderProcess(orderProcess);
				break;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				writer.write(gson.toJson(flag));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 列表
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/orderProcesslist")
	public String findCompanylist(HttpServletRequest request) {
		List list = orderProcessService.findOrderProcessList();
		request.setAttribute("list", list);
		return "sysmanage/OrderPorcess_list";
	}
	
}
